<script setup lang="ts">
import type { Recal } from "@/types/Recall";
import { HomeAPI } from "@/api/HomeAPI";
import { useUserStore } from "@/stores/userStore";

import { recallStore } from "@/stores/recallStore";
//获取用户名store对象
const userStore = useUserStore();
const { userinfo } = storeToRefs(userStore);
//获取用户回忆录store对象
const recallstore = recallStore();
//获取回忆录数据
const { recallist } = storeToRefs(recallstore);
//发送请求获取回忆录数据
recallstore.get_Recall();
// 添加事件的显示和隐藏
const history_dialogVisible = ref(false);
//定义添加文章所需要的变量
const history = ref<Recal>({
  title: "",
  create_date: "",
  content: "",
  drawing: "",
});
//显示
function xianshi() {
  if (!userinfo.value.result.is_superuser) {
    return;
  }
  (history_dialogVisible.value = true),
    (history.value = { title: "", create_date: "", content: "", drawing: "" });
}
//添加回忆录
async function history_data(history: Recal) {
  const response = await recallstore.add_Recall(history);
  history_dialogVisible.value = response;
}
//编辑回忆录
async function set_Recall(nid: string) {
  if (!userinfo.value.result.is_superuser) {
    return;
  }
  history_dialogVisible.value = true;
  history.value = recallist.value.result.filter((item) => item.nid === nid)[0];
}
//删除回忆录
async function delete_recall(nid: string) {
  if (!userinfo.value.result.is_superuser) {
    return;
  }
  await recallstore.delete_Recall(nid);
}
//粘贴配图
function paste_upload(e: any) {
  let clipboardData = e.clipboardData || e.originalEvent.clipboardData;
  let items = clipboardData.items,
    len = items.length,
    blob = null;
  //在items里找粘贴的image,据上面分析,需要循环
  for (let i = 0; i < len; i++) {
    if (items[i].type.indexOf("image") !== -1) {
      //getAsFile() 此方法只是living standard firefox ie11 并不支持
      blob = items[i].getAsFile();
    }
  }
  console.log(blob);

  if (blob !== null) {
    // 读取图片文件
    let reader = new FileReader();

    reader.onload = async (event) => {
      // event.target.result 即为图片的Base64编码字符串
      let base64_str = event.target!.result;
      //可以在这里写上传逻辑 直接将base64编码的字符串上传（可以尝试传入blob对象，看看后台程序能否解析）
      const response = await HomeAPI.uploadImgFromPaste({
        image: base64_str as string,
      });
      history.value.drawing += response.url + "\n";
    };

    reader.readAsDataURL(blob);
    //阻止默认行为即不让剪贴板内容在div中显示出来
    // e.preventDefault();
  }
}
</script>
<template>
  <div class="main">
    <el-dialog
      :title="history.nid ? '事件编辑' : '事件添加'"
      v-model="history_dialogVisible"
    >
      <div class="dialog_content">
        <div>
          <label for="">事件标题</label>
          <el-input
            v-model="history.title"
            placeholder="请输入事件标题"
          ></el-input>
        </div>
        <div>
          <label for="">事件日期</label>
          <el-date-picker
            v-model="history.create_date"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="选择日期"
          >
          </el-date-picker>
        </div>
        <div>
          <label for="">事件内容</label>
          <el-input
            type="textarea"
            :rows="4"
            placeholder="请输入事件内容"
            resize="none"
            v-model="history.content"
          >
          </el-input>
        </div>
        <div>
          <label for="">事件配图</label>
          <textarea
            rows="10"
            class="drawing"
            placeholder="请输入事件配图"
            @paste="paste_upload"
            v-model="history.drawing"
          >
          </textarea>
        </div>
        <div class="drawing_show">
          <img
            :src="url"
            alt=""
            v-for="(url, index) in history.drawing.split('\n')"
            :key="index"
          />
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="history_dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="history_data(history)">
          确 定</el-button
        >
      </span>
    </el-dialog>
    <h2 class="title" @click="xianshi()">染柒博客建站回忆录</h2>
    <ul class="timeline-wrapper">
      <li
        class="timeline-item"
        v-for="(item, index) in recallist.result"
        :key="index"
      >
        <div class="timeline-box">
          <div class="out-circle" @click="delete_recall(item.nid as string)">
            <div class="in-circle"></div>
          </div>
          <div class="long-line"></div>
        </div>
        <div class="timeline-content">
          <div
            class="timeline-date cursor"
            @click="set_Recall(item.nid as string)"
          >
            {{ item.create_date }}
          </div>

          <div class="timeline-title">{{ item.title }}</div>
          <ul class="timeline-desc">
            <li v-for="(content, index) in item.content.split('\n')">
              {{ content }}
            </li>
          </ul>
          <div class="peitu">
            <div>
              <img
                :src="url"
                alt=""
                v-for="(url, index) in item.drawing.split('\n')"
                :key="index"
              />
            </div>
          </div>
        </div>
      </li>
    </ul>
  </div>
</template>

<style lang="scss" scoped>
.main {
  width: 1200px;
  margin: 0 auto;
}
.title {
  margin-bottom: 20px;
}

.dialog_content {
  > div {
    display: flex;
    flex-direction: column;
    margin-bottom: 10px;

    > label {
      display: block;
      margin-bottom: 5px;
    }
  }

  .drawing_show {
    display: inherit;

    img {
      height: 80px;
      border-radius: 5px;
      margin-right: 10px;
      box-shadow: 0 0 5px rgba(0, 0, 0, 0.2);
    }
  }
}

.timeline-wrapper {
  width: 100%;
  list-style: none;
}

.cursor {
  cursor: pointer;
}

/* 时间线 */
.timeline-item {
  position: relative;

  .timeline-box {
    text-align: center;
    position: absolute;
    height: 100%;

    .out-circle {
      width: 16px;
      height: 16px;
      background: rgba(14, 116, 218, 0.1);
      box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.1);
      /*opacity: 0.1;*/
      border-radius: 50%;
      display: flex;
      align-items: center;

      .in-circle {
        width: 8px;
        height: 8px;
        margin: 0 auto;
        background: rgba(14, 116, 218, 1);
        border-radius: 50%;
        box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.1);
        transition: all 0.3s;

        &.super:hover {
          background-color: #dd6161;
        }
      }
    }

    .long-line {
      width: 2px;
      height: calc(100% - 10px);
      background: rgba(14, 116, 218, 1);
      box-shadow: 0 4px 12px 0 rgba(0, 0, 0, 0.1);
      opacity: 0.1;
      margin-left: 8px;
    }
  }

  .timeline-content {
    box-sizing: border-box;
    margin-left: 20px;
    padding: 0 0 0 20px;
    text-align: left;
    margin-bottom: 30px;

    .timeline-title {
      font-size: 15px;
      font-weight: 600;
      word-break: break-all;
      margin-bottom: 10px;
      color: var(--font_2);
      /*display: inline;*/
    }

    .timeline-date {
      font-size: 18px;
      color: var(--font_1);
      font-weight: 500;
      margin-bottom: 10px;
      width: 10rem;
    }

    .timeline-desc {
      font-size: 14px;
      color: var(--font_3);
      list-style: circle;
      padding-left: 20px;

      a {
        color: var(--active);
      }
    }

    .peitu {
      margin-top: 10px;

      > div {
        display: flex;
        flex-wrap: wrap;

        img {
          height: 130px;
          margin-right: 20px;
          border-radius: 5px;
          box-shadow: 1px 1px 10px rgba(0, 0, 0, 0.1);
          margin-bottom: 10px;
        }
      }
    }
  }
}
</style>
